package nocannobb.ucanuup.daringshai.adapter;

import java.util.LinkedList;
import java.util.List;

import nocannobb.ucanuup.daringshai.R;
import nocannobb.ucanuup.daringshai.bitmaputil.ImageFetcher;
import nocannobb.ucanuup.daringshai.model.Beauty;
import nocannobb.ucanuup.daringshai.releaseconst.ReleaseConst;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StaggeredAdapter extends BaseAdapter {
	private LinkedList<Beauty> mBeautys;
	ImageFetcher mImageFetcher;

	public StaggeredAdapter(Context context, ImageFetcher f) {
		mBeautys = new LinkedList<Beauty>();
		mImageFetcher = f;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		Beauty beauty = mBeautys.get(position);

		if (convertView == null) {
			LayoutInflater layoutInflator = LayoutInflater.from(parent
					.getContext());
			convertView = layoutInflator.inflate(R.layout.infos_list, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.news_pic);
			holder.contentView = (TextView) convertView
					.findViewById(R.id.news_title);
			convertView.setTag(holder);
		}
		holder = (ViewHolder) convertView.getTag();

		// float iHeight = ((float) 200 / 183 * duitangInfo.getHeight());
		holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, 1200));

		holder.contentView.setText(beauty.getTitle());
		mImageFetcher.loadImage(ReleaseConst.SERVER_BEAUTY_IMAGE_URL + beauty.getImage_id(), holder.imageView);
		return convertView;
	}

	class ViewHolder {
		ImageView imageView;
		TextView contentView;
		TextView timeView;
	}

	@Override
	public int getCount() {
		return mBeautys.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mBeautys.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	public void addItemLast(List<Beauty> datas) {
		if(datas != null)
			mBeautys.addAll(datas);
	}

	public void addItemTop(List<Beauty> datas) {
		for (Beauty info : datas) {
			mBeautys.addFirst(info);
		}
	}
}
